package com.sora.sora.server.controller;

import com.sora.sora.common.model.vo.ResultVO;
import com.sora.sora.common.utils.ResponseResult;
import com.sora.sora.manager.model.vo.UserVO;
import com.sora.sora.server.service.UserService;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@Api(value = "用户信息模块", tags = "用户信息管理")
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    ResponseResult responseResult;



    @ApiOperation(value = "展示用户基本信息")
    @ApiImplicitParam(name = "uid", value = "用户id", required = true, dataType = "int", paramType = "path")
    @GetMapping("/{uid}")
    public ResultVO show(@PathVariable("uid") int uid) {
        return responseResult.ok(userService.showUser(uid));
    }


    @ApiOperation(value = "用户上传头像")
    @ApiImplicitParam(name = "uid", value = "用户id", required = true, dataType = "int", paramType = "query")
    @PostMapping(value = {"/avatar"}, headers = "content-type=multipart/form-data")
    public ResultVO uploadAvatar(@RequestParam("uid") int uid, MultipartFile file) {
        String name = file.getOriginalFilename();
        if (!name.endsWith(".jpg") && !name.endsWith(".png") && !name.endsWith("./jpeg")) {
            return responseResult.valid("不是正确的图片格式");
        }
        if (file.isEmpty()) {
            return responseResult.valid("传入文件为空");
        } else if (file.getName().trim().equals("") || file.getName() == null) {
            return responseResult.valid("传入文件为空");
        } else if (file.getSize() >= 5*1024*1024) {   // 单位为b   1024b=1kb    1024kb=1m
            return responseResult.valid("图片大小不能超过5mb");
        }

        String url = userService.uploadAvatar(uid, file);
        return url != null ? responseResult.ok(url) : responseResult.faile("更新失败");
    }


    @ApiOperation(value = "用户修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户id", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "用户新密码", required = true, dataType = "string", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "更改成功"),
            @ApiResponse(code = 1002, message = "操作失败")
    })
    @PutMapping("/password")
    public ResultVO resetPassword(@RequestParam("uid") int uid, @RequestParam("password") String password) {
        boolean flag = userService.updatePassword(uid, password);
        return flag ? responseResult.ok() : responseResult.faile();
    }


    @ApiOperation(value = "用户修改邮箱")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户id", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "email", value = "用户新邮箱地址", required = true, dataType = "int", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "更改成功"),
            @ApiResponse(code = 1002, message = "操作失败")
    })
    @PutMapping("/email")
    public ResultVO resetEmail(@RequestParam("uid") int uid, @RequestParam("email") String emial) {
        return null;
    }


    @ApiOperation(value = "用户修改签名")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户id", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "sign", value = "新签名", required = true, dataType = "string", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "更改成功"),
            @ApiResponse(code = 1002, message = "操作失败")
    })
    @PutMapping("/sign")
    public ResultVO resetSign(@RequestParam("uid") int uid, @RequestParam("sign") String sign) {
        boolean flag = userService.updaateSignature(uid, sign);
        return flag ? responseResult.ok() : responseResult.faile();
    }
}
